package com.huawo.qjs;

import android.annotation.SuppressLint;
import android.graphics.Point;
import android.util.Size;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public abstract class QjsWidget implements QjsCycle {

    public static final int NONE = -1;
    public static final int STEPS_NUM = 0;       //步数
    public static final int CALORIE_NUM = 1;     //卡路里
    public static final int HEARTRATE_NUM = 2;   //心率
    public static final int TEMPERATURE_NUM = 4; //气温3;
    public static final int TEMPERATURE_TYPE_NUM = 73; //气温3;
    public static final int BATTERY_NUM = 9;     //电量4;
    public static final int HOUR_NUM = 12;      //时钟
    public static final int MINUTE_NUM = 13;     //分钟
    public static final int SECOND_NUM = 14;     //秒钟
    public static final int HOUR_POINTER = 150;  //时钟比例12
    public static final int MINUTE_POINTER = 153;  //分钟比例
    public static final int SECOND_POINTER = 154;  //秒钟比例
    public static final int WEEK_DATA = 52;      //星期数据
    public static final int DATE_NUM = 17;       //日期
    public static final int DISTANCE_NUM = 146;   //距离
    public static final int WEATHER_NUM = 21;    //天气

    public static final int ACTIVITY_TIME = 214; //运动时长

    private String name;
    private Point location;
    private Size size;

    private int style;
    private int watchfaceStyle;

    private Point imgPoint;
    private int color;
    public int index;

    public QjsWidget(String name) {
        this.name = name;
        this.color = 0xFFFFFF;
    }

    public QjsWidget(String name, int color) {
        this.name = name;
        this.color = color;
    }

    public List<String> getImageNames() {
        return new ArrayList<>();
    }

    public abstract List<QjsDependence> getDependences();

    @Override
    public String toQjs() {
        return null;
    }

    @Override
    public String start() {
        return null;
    }

    @Override
    public String refresh() {
        return null;
    }

    @Override
    public String Resume() {
        return "";
    }

    @Override
    public String pause() {
        return "";
    }

    @Override
    public String stop() {
        return "";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Size getSize() {
        return size;
    }

    public void setSize(Size size) {
        this.size = size;
    }

    public Point getLocation() {
        return location;
    }

    public void setLocation(Point location) {
        this.location = location;
    }

    public int getStyle() {
        return style;
    }

    public void setStyle(int style) {
        this.style = style;
    }

    public int getWatchfaceStyle() {
        return watchfaceStyle;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public Point getImgPoint() {
        return imgPoint;
    }

    public void setImgPoint(Point imgPoint) {
        this.imgPoint = imgPoint;
    }

    public void setWatchfaceStyle(int watchfaceStyle) {
        this.watchfaceStyle = watchfaceStyle;
    }

    @SuppressLint("DefaultLocale")
    public static String groupJS(String nameSuffix, int type, String imagePrefix, Point point, Size size, int iconStyle, int index) {
        nameSuffix = nameSuffix + index;
        StringBuilder sb = new StringBuilder();
        String name = "this.group" + nameSuffix;
        String pathName = "this.gpath" + nameSuffix;
        String fontImagePath = "this.path() + \"" + imagePrefix + "\"";
        // 创建
        sb.append("\n");
        sb.append("\t\t" + pathName + " = " + fontImagePath + ";\n");
        sb.append("\t\t" + name + " = new label(this.root());\n");
        sb.append("\t\t" + name + ".set_long_mode(1);\n");
//        sb.append("\t\t" + name + ".set_pos(" + point.x + "," + point.y + ");\n");

        // 居右
        int align = 2;
        if (nameSuffix.contains("_minute") || nameSuffix.contains("_week")) {
            align = 0;
            sb.append("\t\t" + name + ".set_pos(" + point.x + "," + point.y + ");\n");
        } else if (nameSuffix.contains("_hour") || nameSuffix.contains("_date")) {
            align = 2;
            sb.append("\t\t" + name + ".set_pos(" + point.x + "," + point.y + ");\n");
        } else {
            switch (iconStyle) {
                case 0:
                    align = 1;
                    sb.append("\t\t" + name + ".set_pos(" + (point.x - size.getWidth() / 2 + 20) + "," + (point.y - size.getHeight()) + ");\n");
                    break;
                case 1:
                    align = 1;
                    sb.append("\t\t" + name + ".set_pos(" + (point.x - size.getWidth() / 2 + 20) + "," + (point.y + 40) + ");\n");
                    break;
                case 2:
                    align = 2;
                    sb.append("\t\t" + name + ".set_pos(" + (point.x - size.getWidth()) + "," + (point.y + 7) + ");\n");
                    break;
                case 3:
                    align = 0;
                    sb.append("\t\t" + name + ".set_pos(" + (point.x + 40) + "," + (point.y + 7) + ");\n");
                    break;
            }
        }
        sb.append("\t\t" + name + ".set_size(" + size.getWidth() + "," + size.getHeight() + ");\n");

        // 2023-05-17 杨浩：图片字体也不用set_local_font了
        sb.append("\t\t" + name + ".set_align(" + align + ");\n");
        sb.append("\t\t" + name + ".set_font_image(0, 0, " + pathName + ", 0);\n");

        // 绑定数据
        String text_name = "this.gtext" + nameSuffix;
        String t = "\"%\"";
        String f = "\"℉\"";
        String c = "\"℃\"";
        // 初始化值
        sb.append(String.format(Locale.ENGLISH,"\t\t%s = lv.app_db_get_val_text(%d);\n", text_name, type));
        if (nameSuffix.contains("_battery")) {
            sb.append(String.format(Locale.ENGLISH,"\t\t%s.set_text(%s + %s);\n", name, text_name, t));
        } else if (nameSuffix.contains("_weater")) {
            sb.append(String.format(Locale.ENGLISH,"\t\tthis.temp_unit%d = lv.app_db_get_val_enum(%d);\n", index, TEMPERATURE_TYPE_NUM));
            sb.append(String.format(Locale.ENGLISH,"\t\tif(this.temp_unit%d == 1) {\n\t\t\t%s.set_text(%s + %s);\n\t\t}\n", index, name, text_name, f));
            sb.append(String.format(Locale.ENGLISH,"\t\telse {\n\t\t\t%s.set_text(%s + %s);\n\t\t}\n\n", name, text_name, c));
        } else {
            sb.append(String.format(Locale.ENGLISH,"\t\t%s.set_text(%s);\n", name, text_name));
        }
        // 绑定数据
        sb.append(String.format(Locale.ENGLISH,"\t\t%s.bind(%d, function(idx, val)", name, type));
        sb.append(" {\n");
        sb.append(String.format(Locale.ENGLISH,"\t\t\t%s = lv.app_db_get_val_text(%d);\n", text_name, type));
        if (nameSuffix.contains("_battery")) {
            sb.append(String.format(Locale.ENGLISH,"\t\t\tthis.set_text(%s + %s);\n", text_name, t));
        } else if (nameSuffix.contains("_weater")) {
            sb.append(String.format(Locale.ENGLISH,"\t\t\tthis.temp_unit%d = lv.app_db_get_val_enum(%d);\n", index, TEMPERATURE_TYPE_NUM));
            sb.append(String.format(Locale.ENGLISH,"\t\t\tif(this.temp_unit%d == 1) {\n\t\t\t\tthis.set_text(%s + %s);\n\t\t\t}\n", index, text_name, f));
            sb.append(String.format(Locale.ENGLISH,"\t\t\telse {\n\t\t\t\tthis.set_text(%s + %s);\n\t\t\t}\n", text_name, c));
        } else {
            sb.append(String.format(Locale.ENGLISH,"\t\t\tthis.set_text(%s);\n", text_name));
        }
        sb.append("\t\t});\n");

        return sb.toString();
    }

    public static String imgJS(String nameSuffix, String binPath, Point point, int index) {
        nameSuffix = nameSuffix + index;
        StringBuilder sb = new StringBuilder();
        String name = "this.img" + nameSuffix;
        String path = "this.path() + \"" + binPath + "\"";
        sb.append("\n");
        sb.append("\t\t" + name + " = new img(this.root());\n");
        sb.append("\t\t" + name + ".set_src(" + path + ");\n");
        sb.append("\t\t" + name + ".set_pos(" + point.x + "," + point.y + ");");
        sb.append("\n");
        return sb.toString();
    }

    public static String gifJS(String nameSuffix, String binPath, String binffPath, Point point, int index) {
        nameSuffix = nameSuffix + index;
        StringBuilder sb = new StringBuilder();
        String gif = "this.gif"+index;
        String gif_src = "this.gif" + nameSuffix + "_src";
        String gif_ff_src = "this.gif" + nameSuffix + "ff_src";
        String agif = "this.path() + \""+binPath+"\"";
        String gif_bin = "this.path() + \""+binffPath+"\"";

        sb.append("\n");
        sb.append("\t\t\t" + gif_src + " = " + agif + ";\n");
        sb.append("\t\t" + gif_ff_src + " =  " + gif_bin + ";\n");
        sb.append("\t\t" + gif + " = new gif(this.root());\n");
        sb.append("\t\t" + gif + ".anim_open(" + gif_src + ", " + gif_ff_src + ", 0, 0, 0, 100);");
        sb.append("\n");
        return sb.toString();
    }

    @SuppressLint("DefaultLocale")
    public static String optionJS(String nameSuffix, int type, String imagePrefix, Point point, int index) {
        nameSuffix += index;
        StringBuilder sb = new StringBuilder();
        String name = "this.option" + nameSuffix;
        // 创建
        sb.append("\n");
        sb.append("\t\t" + name + " = new idximg(this.root());\n");
        sb.append("\t\t" + name + "." + "set_pos(" + point.x + "," + point.y + ");\n");
        sb.append("\t\t" + name + "." + "prefix(this.path() + \"" + imagePrefix + "\");\n");

        // 绑定数据
        String index_name = "this.pe" + nameSuffix;
        // 初始化值
        sb.append(String.format(Locale.ENGLISH,"\t\t%s = lv.app_db_get_val_enum(%d);\n", index_name, type));
        sb.append(String.format(Locale.ENGLISH,"\t\t%s.select(%s);\n", name, index_name));
        // 绑定数据
        sb.append(String.format(Locale.ENGLISH,"\t\t%s.bind(%d, function(idx, val)", name, type));
        sb.append(" {\n");
        sb.append(String.format(Locale.ENGLISH,"\t\t\t%s = lv.app_db_get_val_enum(%d);\n", index_name, type));
        sb.append(String.format(Locale.ENGLISH,"\t\t\tthis.select(%s);\n", index_name));
        sb.append("\t\t});\n");

        return sb.toString();
    }

    public static String groupResumeJs(String nameSuffix, int type, String imagePrefix, Point point, Size size, int iconStyle, int index) {
        String newNameSuffix = nameSuffix + index;
        StringBuilder sb = new StringBuilder();
        String name = "this.group" + newNameSuffix;
        //创建
        sb.append("\n");

        //绑定数据
        String textName = "this.gtext" + newNameSuffix;
        String t = "\"%\"";
        String f = "\"℉\"";
        String c = "\"℃\"";
        // 初始化值
        sb.append(String.format(Locale.ENGLISH,"\t\t%s = lv.app_db_get_val_text(%d);\n", textName, type));
        if (newNameSuffix.contains("_battery")) {
            sb.append(String.format(Locale.ENGLISH,"\t\t%s.set_text(%s + %s);\n", name, textName, t));
        } else if (newNameSuffix.contains("_weater")) {
            sb.append(String.format(Locale.ENGLISH,"\t\tthis.temp_unit%d = lv.app_db_get_val_enum(%d);\n", index, TEMPERATURE_TYPE_NUM));
            sb.append(String.format(Locale.ENGLISH,"\t\tif(this.temp_unit%d == 1) {\n\t\t\t%s.set_text(%s + %s);\n\t\t}\n", index, name, textName, f));
            sb.append(String.format(Locale.ENGLISH,"\t\telse {\n\t\t\t%s.set_text(%s + %s);\n\t\t}\n\n", name, textName, c));
        } else {
            sb.append(String.format(Locale.ENGLISH,"\t\t%s.set_text(%s);\n", name, textName));
        }
        return sb.toString();
    }

    public static String optionResumeJs(String nameSuffix, int type, String imagePrefix, Point point, int index) {
        String newNameSuffix = nameSuffix + index;
        StringBuilder sb = new StringBuilder();
        String name = "this.option" + newNameSuffix;
        // 创建
        sb.append("\n");
        // 绑定数据
        String index_name = "this.pe" + newNameSuffix;
        // 初始化值
        sb.append(String.format(Locale.ENGLISH,"\t\t%s = lv.app_db_get_val_enum(%d);\n", index_name, type));
        sb.append(String.format(Locale.ENGLISH,"\t\t%s.select(%s);\n", name, index_name));
        return sb.toString();
    }

    public static String pointerResumeJs(String nameSuffix, int type, String binPath, int index) {
        String newNameSuffix = nameSuffix + index;
        StringBuilder sb = new StringBuilder();
        String name = "this.pointer" + newNameSuffix;
        // 创建
        sb.append("\n");
        // 绑定数据
        String percent_name = "this.pp" + newNameSuffix;
        // 初始化值
        sb.append(String.format(Locale.ENGLISH,"\t\t%s = lv.app_db_get_val_percent(%d);\n", percent_name, type));
        sb.append(String.format(Locale.ENGLISH,"\t\t%s.set_value(%s*3600/10000);\n", name, percent_name));
        return sb.toString();
    }

    @SuppressLint("DefaultLocale")
    public static String pointerJS(String nameSuffix, int type, String binPath,int width,int height) {

        Point pos = new Point();
        Point center = new Point();
        if (width == 410) {
            if (type == HOUR_POINTER) {
                pos.set(205, 119 + 5);
                center.set(7, 114);
            } else if (type == MINUTE_POINTER) {
                pos.set(205, 39 + 5);
                center.set(7, 194);
            } else {
                pos.set(210, 11 + 5);
                center.set(3, 222);
            }
        }else{
            if (type == HOUR_POINTER) {
                pos.set(226, 119);
                center.set(7, 114);
            } else if (type == MINUTE_POINTER) {
                pos.set(226, 39);
                center.set(7, 194);
            } else {
                pos.set(230, 11);
                center.set(3, 222);
            }
        }


        StringBuilder sb = new StringBuilder();
        String name = "this.pointer" + nameSuffix;
        // 创建
        String path = "this.path() + \"" + binPath + "\"";
        sb.append("\n");
        sb.append("\t\t" + name + " = new pointer(this.root());\n");
        sb.append("\t\t" + name + ".set_src(" + path + ");\n");
        sb.append("\t\t" + name + "." + "set_range(0, 3600);\n");
        sb.append("\t\t" + name + "." + "set_pos(" + pos.x + "," + pos.y + ");\n");
        sb.append("\t\t" + name + "." + "set_pivot(" + center.x + "," + center.y + ");\n");

        // 绑定数据
        String percent_name = "this.pp" + nameSuffix;
        // 初始化值
        sb.append(String.format(Locale.ENGLISH,"\t\t%s = lv.app_db_get_val_percent(%d);\n", percent_name, type));
        sb.append(String.format(Locale.ENGLISH,"\t\t%s.set_value(%s*3600/10000);\n", name, percent_name));
        // 绑定数据
        sb.append(String.format(Locale.ENGLISH,"\t\t%s.bind(%d, function(idx, val)", name, type));
        sb.append(" {\n");
        sb.append(String.format(Locale.ENGLISH,"\t\t\t%s = lv.app_db_get_val_percent(%d);\n", percent_name, type));
        sb.append(String.format(Locale.ENGLISH,"\t\t\tthis.set_value(%s*3600/10000);\n", percent_name));
        sb.append("\t\t});\n");

        return sb.toString();
    }

    @Override
    public String toString() {
        return "QjsWidget{" +
                "name='" + name + '\'' +
                ", size=" + size +
                ", style=" + style +
                ", watchfaceStyle=" + watchfaceStyle +
                ", imgPoint=" + imgPoint +
                ", color=" + color +
                '}';
    }
}
